/**
 * @brief get a list of selected resumes
 * @param string property (option) the property of the resume requested
 * @return array
 *
 * possible values for property: id, name, mime and type
 * if property is set, an array with that property for each resume is returnd
 * if it's ommited an array of objects with all properties is returned
 */
function getSelectedResumes(property){
   var elements=$('td.resumename input:checkbox:checked').parent().parent().parent();
   var resumes=[];
   /* make sure to filter out just-trashed resumes */
   elements.not(function() { return $(this).hasClass("trashed"); }).each(function(i,element){
      if($.isArray(property)){
         var resume={};
         $.each(property,function(index,prop){
            resume[prop]=$(element).data(prop);
         });
         resumes.push(resume);
      }else{
         var resume={
            id:$(element).attr('data-id'),
            name:$(element).attr('data-name'),
            mime:$(element).data('mime'),
            type:$(element).data('type')
         };
         if(property){
            resumes.push(resume[property]);
         }else{
            resumes.push(resume);
         }
      }
   });
   return resumes; 
}

function procesSelection(){
   var selectedResumes=getSelectedResumes();
   if(selectedResumes.length==0) {
      $('#headerName>span.name').html('姓名 / 信息');
      $('#headerName').nextAll('th').find("span").show();
      $('table.pipapai').removeClass('multiselect');
      $('table.pipapai .selectedActions').hide();
   }
   else {
      $('table.pipapai ').addClass('multiselect');
      $('#headerName').nextAll('th').find("span").hide(); 

      var selection='已选择';
      if(selectedResumes.length>0){
         if(selectedResumes.length==1){
            selection+=t('resumes','1 resume');
         }else{
            selection+=t('resumes','{count} resumes',{count: selectedResumes.length});
         }
      }
      $('#headerName>span.name').text(selection);
      $('table.pipapai .selectedActions').show();
   }
}

jQuery(document).ready(function($) {
   Resumes.bindKeyboardShortcuts(document, jQuery);
   procesSelection();

   var lastChecked;

   // Sets the file link behaviour :
   $('#resumeList').on('click','td.resumename a',function(event) {
      if (event.ctrlKey || event.shiftKey) {
         event.preventDefault();
         if (event.shiftKey) {
            var last = $(lastChecked).parent().parent().parent().prevAll().length;
            var first = $(this).parent().parent().parent().prevAll().length;
            var start = Math.min(first, last);
            var end = Math.max(first, last);
            var rows = $(this).parent().parent().parent().parent().children('tr');
            for (var i = start; i < end; i++) {
               $(rows).each(function(index) {
                  if (index == i) {
                     var checkbox = $(this).children().children().children('input:checkbox');
                     $(checkbox).prop('checked', true);
                     $(checkbox).parent().parent().parent().addClass('selected');
                     $(checkbox).parent().parent().parent().next().addClass('selected');
                  }
               });
            }
         }
         var checkbox = $(this).parent().children('input:checkbox');
         lastChecked = checkbox;
         if ($(checkbox).prop('checked')) {
            $(checkbox).prop('checked', false);
            $(checkbox).parent().parent().parent().removeClass('selected');
            $(checkbox).parent().parent().parent().next().removeClass('selected');
            $('#select_all').prop('checked', false);
         } else {
            $(checkbox).prop('checked', true); 
            $(checkbox).parent().parent().parent().toggleClass('selected');
            var selectedCount=$('td.resumename input:checkbox:checked').length;
            if (selectedCount == $('td.resumename input:checkbox').length) {
               $('#select_all').prop('checked', true);
            }
         }
         procesSelection();
      } else {
         var resume=$(this).parent().parent().parent().attr('data-id');
         var tr=$('tr').filterAttr('data-id',resume);
         ResumeActions.currentResume = $(this).parent().parent();
         var mime=ResumeActions.getCurrentMimeType();
         var type=ResumeActions.getCurrentType();
         var permissions = ResumeActions.getCurrentPermissions();
         var action=ResumeActions.getDefault(mime,type, permissions);
         if(action){
            event.preventDefault();
            action(resume);
         }
      }

   });

   function scrollmessage_r() {
      var scrolltops = $(window).scrollTop();
      var searchheight = $(".highsearch_wrap").height()+25;
      if (scrolltops >= (searchheight+4)) {
         $(".hr_resume_tablebox table.multiselect.slidedown thead").addClass("needfixed");
      }
      else {
         $(".hr_resume_tablebox table.multiselect.slidedown thead").removeClass("needfixed");
      } 
   }

   $(window).scroll(function(){
      scrollmessage_r();
   });

   // Sets the select_all checkbox behaviour :
   $('#select_all').click(function() {
      if($(this).prop('checked')){
         // Check all
         $('td.resumename input:checkbox').prop('checked', true);
         $('td.resumename input:checkbox').parent().parent().parent().addClass('selected');
         $('td.resumename input:checkbox').parent().parent().parent().next().addClass('selected');
      }else{
         // Uncheck all
         $('td.resumename input:checkbox').prop('checked', false);
         $('td.resumename input:checkbox').parent().parent().parent().removeClass('selected');
         $('td.resumename input:checkbox').parent().parent().parent().next().removeClass('selected');
      }
      procesSelection();
      if(window.setPosition_2){
         wintop = $(window).scrollTop();
         setPosition_2();
      }

      scrollmessage_r();
   });

   $('#resumeList').on('change', 'td.resumename input:checkbox',function(event) {
      if (event.shiftKey) {
         var last = $(lastChecked).parent().parent().parent().prevAll().length;
         var first = $(this).parent().parent().parent().prevAll().length;
         var start = Math.min(first, last);
         var end = Math.max(first, last);
         var rows = $(this).parent().parent().parent().parent().children('tr');
         for (var i = start; i < end; i++) {
            $(rows).each(function(index) {
               if (index == i) {
                  var checkbox = $(this).children().children().children('input:checkbox');
                  $(checkbox).prop('checked', true);
                  $(checkbox).parent().parent().parent().addClass('selected');
                  $(checkbox).parent().parent().parent().next().addClass('selected');
               }
            });
         }
      }
      var selectedCount=$('td.resumename input:checkbox:checked').length;
      $(this).parent().parent().parent().toggleClass('selected');
      $(this).parent().parent().parent().next().toggleClass('selected');
      if(!$(this).prop('checked')){
         $('#select_all').prop('checked',false);
      }else{
         if(selectedCount==$('td.resumename input:checkbox').length){
            $('#select_all').prop('checked',true);
         }
      }
      procesSelection();
      if(window.setPosition_2){
         wintop = $(window).scrollTop();
         setPosition_2();
      }

      scrollmessage_r();
   });

   $('#headerName').on('click', '.download', function(event) {
      var resumes = getSelectedResumes('id').join(',');
      OC.Notification.show(t('resumes','Your download is being prepared. This might take some time if the resumes are big.'), 5000);
      // use special download URL if provided, e.g. for public shared files
      if ( (downloadURL = document.getElementById("downloadURL")) ) {
         window.location=downloadURL.value+"&download&ids="+resumes;
      } else {
         window.location=OC.filePath('resumes', 'ajax', 'download.php') + '?ids='+ resumes; 
      }
      
      // when download finish, hide notification
      var downloadObj = window.setInterval(function(){
         if ($.cookie('DOWNLOAD_DONE')) {
            OC.Notification.hide();
            $.cookie('DOWNLOAD_DONE', null, {path:'/'});
            if($.cookie('DOWNLOAD_DELAYED')) {
               $.cookie('DOWNLOAD_DELAYED', null, {path:'/'});
               if($.cookie('DOWNLOAD_DELAYED_ERROR')) {
                  $.cookie('DOWNLOAD_DELAYED_ERROR', null, {path:'/'});
                  OC.Notification.show("您的下载请求被拒绝，请稍后再试！", 3000);
               } else if($.cookie('DOWNLOAD_DELAYED_DUPLICATE')) {
                  $.cookie('DOWNLOAD_DELAYED_DUPLICATE', null, {path:'/'});
                  OC.Notification.show("您已有一个相似的请求在任务列表里！请删除以前的下载任务，再点击下载按钮！", 5000);
               }
               else {
                  OC.Notification.show("您的下载请求已加入到任务列表里，请稍后查看状态通知！", 5000); 
                  $("#psysnotify").trigger("showtask");
               }
            }
            window.clearInterval(downloadObj);
            return;
         }
      }, 1000);
      
      return false;
   });

   $('#headerName').on('click', '.delete', function(event) {
      var resumes=getSelectedResumes('id');
      event.preventDefault();
      ResumeList.do_trash(resumes);
      return false;
   });

   $('#headerName').on('click', '.save', function(event) {
      var resumes=getSelectedResumes('id');
      event.preventDefault();
      ResumeList.do_save(resumes);
      return false;
   });

   $('#headerName').on('click', '.unsave', function(event) {
      var resumes=getSelectedResumes('id');
      event.preventDefault();
      ResumeList.do_unsave(resumes);
      return false;
   });

   $('#headerName').on('click', '.restore', function(event) {
      var resumes=getSelectedResumes('id');
      event.preventDefault();
      ResumeList.do_restore(resumes);
      return false;
   });

   $('#headerName').on('click', '.permdelete', function(event) {
      var resumes=getSelectedResumes('id');
      event.preventDefault();
      ResumeList.do_permdelete(resumes);
      return false;
   });
   
   $('#headerName').on('click', '.invite', function(event) {
      var resumes=getSelectedResumes(['id', 'href', 'jobsystem', 'issetup']);
      event.preventDefault();
      ResumeList.do_invite(resumes);
      return false;
   });
   
   $('#headerName').on('click', '.infolder', function(event) {
      var resumes=getSelectedResumes('id');
      event.preventDefault();
      ResumeList.do_infolder(resumes);
      return false;
   });

   // sort the resume list 
   $('.resume_listtable thead th a.sort').filterAttr('data-orderby', $('#orderBy').val()).each(function() {
      $(this).parent().parent().addClass('selected').find('i.downblack').addClass($('#orderDir').val().toLowerCase()); 
   });

   $('.resume_listtable thead th').on('click', 'a.sort', function() {
      var neworderby = $(this).data('orderby'); 
      var neworderdir = $(this).data('orderdir'); 
      var orderby = $('#orderBy').val(); 
      var orderdir = $('#orderDir').val(); 
      var topnlist = $(this).hasClass("topnlist") ? 1 : 0;
      $.ajax({
         type: "POST",
         url: OC.filePath('resumes', 'ajax', 'sortlist.php'),
         data: {neworderby: neworderby, neworderdir: neworderdir, orderby: orderby, orderdir: orderdir, topnlist:topnlist},
         dataType: 'json',
         success: function(result){
            var url = window.location.href,  
                ts = $.now(),
                ret = url.replace( /([?&])_=[^&]*/, "$1_=" + ts );                
            window.location = ret + ( ( ret === url ) ? ( /\?/.test(url ) ? "&" : "?" ) + "_=" + ts : "" ); 
         }
      })
   });
   
   // show persontest result
   $('#resumeList').on('click', '.online_interview.persontest', function() {
      if(!$(this).hasClass('gray')) {
         var target = $('#persontest_result');
         var target2 = $('#persontest_noresult');
         var uname = $(this).closest('tr').attr('data-name');
         var jobseeker_id = $(this).closest('tr').attr('data-id');
         $.ajax({
            type: "GET",
            url: OC.filePath('resumes', 'ajax', 'getpersontestresult.php'),
            data: {uname: uname, jobseeker_id: jobseeker_id},
            success: function(result){
               var scrolltops = $(window).scrollTop();  
               if(/id="modal_noresult"/.test(result)){
                  target2.html(result).css("top",scrolltops+75+"px").modal();
               }else{
                  target.html(result).css("top",scrolltops+75+"px").modal();
               }
               $('.person_putsumer_f a').tooltip();              
            }
         });
      }
   });
   
   //fix persontest modal div caching issue 
   $('#persontest_result').on('show', function () {
      $(this).css('visibility', 'visible');
   });
   
   $('#persontest_result').on('hidden', function () {
      $(this).css('visibility', 'hidden').show();
   });

   //breadcrumbs wrap
   if($('div.crumb').length > 0) {
      //if the breadcrumb is to long, start by replacing foldernames with '...' except for the current folder
      var crumb=$('div.crumb').first();
      while($('div.controls').height()>50 && crumb.next('div.crumb').length>0){
         crumb.children('a').text('...');
         crumb=crumb.next('div.crumb');
      }
      //if that isn't enough, start removing items from the breacrumb except for the current folder and it's parent
      var crumb=$('div.crumb').first();
      var next=crumb.next('div.crumb');
      while($('div.controls').height()>50 && next.next('div.crumb').length>0){
         crumb.remove();
         crumb=next;
         next=crumb.next('div.crumb');
      }
      //still not enough, start shorting down the current folder name
      var crumb=$('div.crumb>a').last();
      while($('div.controls').height()>50 && crumb.text().length>6){
         var text=crumb.text()
         text=text.substr(0,text.length-6)+'...';
         crumb.text(text);
      }
   
      var lastWidth = 0;
      var breadcrumbs = [];
      var breadcrumbsWidth = 0;
      if ($('#navigation').length > 0 ) {
         breadcrumbsWidth = $('#navigation #apps').get(0).offsetWidth;
      }
   
      var hiddenBreadcrumbs = 0;
   
      $.each($('.crumb'), function(index, breadcrumb) {
         breadcrumbs[index] = breadcrumb;
         breadcrumbsWidth += $(breadcrumb).get(0).offsetWidth;
      });
   
   
      $.each($('#controls .actions>div'), function(index, action) {
         breadcrumbsWidth += $(action).get(0).offsetWidth;
      });
   
      function resizeBreadcrumbs(firstRun) {
         var width = $(this).width();
         if (width != lastWidth) {
            if ((width < lastWidth || firstRun) && width < breadcrumbsWidth) {
               if (hiddenBreadcrumbs == 0) {
                  breadcrumbsWidth -= $(breadcrumbs[1]).get(0).offsetWidth;
                  $(breadcrumbs[1]).find('a').hide();
                  $(breadcrumbs[1]).append('<span>...</span>');
                  breadcrumbsWidth += $(breadcrumbs[1]).get(0).offsetWidth;
                  hiddenBreadcrumbs = 2;
               }
               var i = hiddenBreadcrumbs;
               while (width < breadcrumbsWidth && i > 1 && i < breadcrumbs.length - 1) {
                  breadcrumbsWidth -= $(breadcrumbs[i]).get(0).offsetWidth;
                  $(breadcrumbs[i]).hide();
                  hiddenBreadcrumbs = i;
                  i++
               }
            } else if (width > lastWidth && hiddenBreadcrumbs > 0) {
               var i = hiddenBreadcrumbs;
               while (width > breadcrumbsWidth && i > 0) {
                  if (hiddenBreadcrumbs == 1) {
                     breadcrumbsWidth -= $(breadcrumbs[1]).get(0).offsetWidth;
                     $(breadcrumbs[1]).find('span').remove();
                     $(breadcrumbs[1]).find('a').show();
                     breadcrumbsWidth += $(breadcrumbs[1]).get(0).offsetWidth;
                  } else {
                     $(breadcrumbs[i]).show();
                     breadcrumbsWidth += $(breadcrumbs[i]).get(0).offsetWidth;
                     if (breadcrumbsWidth > width) {
                        breadcrumbsWidth -= $(breadcrumbs[i]).get(0).offsetWidth;
                        $(breadcrumbs[i]).hide();
                        break;
                     }
                  }
                  i--;
                  hiddenBreadcrumbs = i;
               }
            }
            lastWidth = width;
         }
      }
      
      $(window).resize(function() {
         resizeBreadcrumbs(false);
      });
   
      resizeBreadcrumbs(true);
   }

//   jQuery(".control_trselect td").not(".resumename,.resumestatus,.resumetest,.resumeactions,.resumematch").click(function() {
//      $(this).parent().next().find('.relative div.clicktoggle').trigger('click');
//   });
//   
//   jQuery(".control_tr td").click(function() {
//      $(this).find('.relative div.clicktoggle').trigger('click');
//   });
   
   $('.resumename span.clicktoggle').click(function(){
      var target = $(this).closest('.resumename').parent().next().find('.resumedetails');
      if(target.is(':hidden')) {
      	//$(this).addClass("togglebutton");
         var resumeid = $(this).closest('tr').attr('data-id');
         $.metadata.setType('attr', 'data');
         var data = $('#resumesearch_wrap').metadata();
         var searchurl = data.searchurl;
         if(!target.html()) {
         	$.ajax({
               type: "GET",
               url: OC.filePath('resumes', 'ajax', 'getresumedetail.php'),
               data: {resumeid: resumeid, searchurl: searchurl},
               dataType: 'html',
               success: function(result){
                  target.html(result);
                  target.show();
               }
            });
         } else {
            target.show();
         }
          //$(this).show();
	      $(this).addClass('clicktoggle_up');
         var ppparent = $(this).parent().parent().parent(); 
	      ppparent.addClass('tr_hovercolor');
	      ppparent.next().removeClass('tdnoborder_bottom');
	      ppparent.addClass('tdnoborder_bottom');
	      ppparent.next().find(".resume_detailtd").css("borderBottom","1px solid #88a5b2");
	      //$(this).attr("data-original-title","点击收起");
      } else {
	   	target.hide();
	   	//$(this).hide();
		   $(this).removeClass('clicktoggle_up');
		   //$(this).removeClass('togglebutton');
         var ppparent = $(this).parent().parent().parent(); 
		   ppparent.removeClass('tr_hovercolor');
		   ppparent.next().addClass('tdnoborder_bottom');
		   ppparent.removeClass('tdnoborder_bottom');
		   ppparent.next().find(".resume_detailtd").css("borderBottom","0px");
		   //$(this).attr("data-original-title","点击查看详情");
      }
   });
   
   $('.resumestatus span,#headerResumeStatus span').click(function(){
      $(this).find('.resumestatus_option').toggle();
   });
   
   /* not use for now
   $('.resume_status_option_child').click(function(){
      var target = $(this);
      var resumeid = target.closest('tr').attr('data-id');
      var status_id = target.attr('data-status-id');
      var status_text = target.text();
      
      $.ajax({
         type: "POST",
         url: OC.filePath('resumes', 'ajax', 'updatestatus.php'),
         data: {resumeid: resumeid, status_id: status_id},
         dataType: 'html',
         success: function(result){
            target.closest('.resume_status_select').find('em.resume_status_text').html(status_text);
         }
      });
   });
   */

   $('#resumeList').delegate(".match_degree_mask.matched", "click", function(e) {
      e = $.event.fix(e);
      var otarget = $(e.target);
      
      if(otarget.closest(".matching_popup").length > 0) {
          if(otarget.hasClass("closediv"))
             otarget.closest(".matching_popup").hide();
           return false;
      }
      var target = $(this); 
      var resumeid = target.closest('tr').attr('data-id');
      
      var parentdiv = $('#matchdetails_'+resumeid); 
      if (parentdiv.length > 0) {
         if(parentdiv.css("display") != 'none') {
            parentdiv.hide();
         }
         else {
            $('.matching_popup').hide();
            parentdiv.show();
         }
      } else {
         $.ajax({
            type: "GET",
            url: OC.filePath('resumes', 'ajax', 'getmatchdetail.php'),
            data: {resumeid: resumeid},
            dataType: 'html',
            cache: false, 
            success: function(result){
               $('.matching_popup').hide();
               target.append("<div class='matching_popup popup' id='matchdetails_"+resumeid+"' style='position:absolute;display:none'></div>");
               $('#matchdetails_'+resumeid).html(result).show();
            }
         });
      }
    
   });
   
   // job and tag filter
   $('.comselectmenubox .needp_absolute a').click(function(){
      $(this).addClass('cur_a').siblings().removeClass('cur_a');
   });
   
   var searchLinksInit = function() {
      $.metadata.setType('attr', 'data');
      var data = $('#resumesearch_wrap').metadata();
      var jobListUrl = joblist_url;
      var tagListUrl = taglist_url;
      
      $('#folder_dropdown').on('click', '.showlist', function() {
         $('#foldermanage_modal').modal('show');
         $.ajax({
            url: OC.filePath('resumes', 'ajax', 'getcustomfolderlist.php'),
            data: {searchurl: data.searchurl},
            beforeSend: function(xhr, settings) {
               showLoading($("#foldermanage_body"));
            },
            success: function(data) {
               $("#foldermanage_body").html(data);
            }
         });
      });
      
      $('#joblinks').on('click', '.showlist', function() {
         $(this).jobSelect({
            title: '选择职位',
            listurl: jobListUrl,
            data: {searchurl: data.searchurl, dir: data.dir}
         });
      });
      
      $('#taglinks').on('click', '.showlist', function() {
         $(this).jobSelect({
            title: '选择标签',
            listurl: tagListUrl,
            data: {searchurl: data.searchurl, dir: data.dir}
         });
      });
   }
   
   searchLinksInit();

   // search module
   
   /* Frozen now
   $('#search_dir').click(function(){
      $(this).find('.select_option').toggle();
   });
   
   $('#search_dir').mouseleave(function(){
      $(this).find('.select_option').hide();
   });
   
   $('#search_dir .select_option a').click(function(){
      var target = $(this);
      var dir = target.attr('data-dir');
      var dir_text = target.text();
      
      target.parent().hide();
      $('#search_dir').attr('data-dir', dir).find('em').text(dir_text);
      return false;
   });
   
   $('#simplesearch').click(function(){
      var keyword = $.trim($('#simplesearchkeyword').focus().val());
      
      if(value.length <= 0)
         return false;
      
      var dir = $('#search_dir').attr('data-dir');
      var simplesearchurl = $(this).attr('data-simplesearchurl');
      window.location = simplesearchurl.replace('%dir', encodeURIComponent(dir)).replace('%keyword', encodeURIComponent(keyword));
   });
   
   $('#simplesearchkeyword').keydown(function(e) {
      if(e.which == 13) {
         e.preventDefault();
         $('#simplesearch').trigger('click');
      }
   });
   */
   
   // function loadAdvancedSearch(callback) {
   //    var target = $('#resumesearch_wrap');
   //    var resumeid = $(this).closest('tr').attr('data-id');
   //    $.metadata.setType('attr', 'data');
   //    var data = target.metadata();
   //    var searchurl = data.searchurl;
   //    var jobpos = data.jobpos;
   //    $.ajax({
   //       type: "GET",
   //       url: OC.filePath('resumes', 'ajax', 'getadvancedsearch.php'),
   //       data: {resumeid: resumeid, searchurl: searchurl, jobpos: jobpos},
   //       dataType: 'html',
   //       beforeSend: function(xhr, settings) {
   //          showLoading(target);
   //       },
   //       success: function(result){
   //          target.html(result);
   //          if($.isFunction(callback)) {
   //             callback();
   //          }
   //       }
   //    });
   // }
   
   // function switchAdvancedSearch(key, val) {
   //    if(key == 'showsearch' || key == 'showmorejob' || 'showdetail') {
   //       $.ajax({
   //          type: 'POST',
   //          url: OC.filePath('resumes', 'ajax', 'switchsearch.php'),
   //          data: {key: key, val: val},
   //          dataType: 'json',
   //          success: function(result){
   //             // do nothing
   //          }
   //       });
   //    }
   // }

   // 原来通过点击按钮ajax加载，现在改为直接加载
   // loadAdvancedSearch();
   // switchAdvancedSearch('showsearch', 1);
   
   // $('#advancedSearch').click(function(){
   //    var target = $('#resumesearch_wrap');
   //    if(target.is(':hidden')) {
   //       if(!target.html()) {
   //          loadAdvancedSearch(function(){
   //             $('label.key', target).each(function(i, el) {
   //                beautifyTableCaption(el);
   //             });
   //             target.show();
   //             switchAdvancedSearch('showsearch', 1);
   //             $(".resume_listtable").addClass("slidedown");
   //             $(".hr_resume_tablebox").css("paddingBottom","350px");
   //             $(".hr_resume_tablebox").siblings(".hr_position_nodata").css("marginTop","0px");
   //          });
   //       } else {
   //          target.show();
   //          switchAdvancedSearch('showsearch', 1);
   //          $(".resume_listtable").addClass("slidedown");
   //          $(".hr_resume_tablebox").css("paddingBottom","350px");
   //          $(".hr_resume_tablebox").siblings(".hr_position_nodata").css("marginTop","0px");
   //       }
   //       $(this).addClass('lowsearch');
   //    } else {
   //       target.hide();
   //       $(this).removeClass('lowsearch');
   //       switchAdvancedSearch('showsearch', 0);
   //       $(".resume_listtable").removeClass("slidedown");
   //       $("#pagination").removeClass("slidedown");
   //       $(".hr_resume_tablebox").css("paddingBottom","105px");
   //       $(".hr_resume_tablebox").siblings(".hr_position_nodata").css("marginTop","-35px");
   //    }
   //    //$('#simplesearchkeyword').focus().blur();
   // });
   
   // $('#resumesearch_wrap').on('click', '.click_slideup', function(){
   //    $('#advancedSearch').trigger('click');
   // });
   
   /* Frozen now
   function buildJobLinks(){
      if($('#joblinks .links').length > 0) {
         $('#joblinks .links').height('auto');
         
         var row_h = $('#joblinks .links li').eq(0).height() + 4;
         var links_h = $('#joblinks .links').height();
         
         if(links_h > (row_h*1.5)) {
            $('#joblinks .select_more').show();
         } else {
            $('#joblinks .select_more').hide();
         }
         
         if($('#joblinks .links').hasClass('showmorejob')) {
            $('#joblinks .links').height('auto');
         } else {
            $('#joblinks .links').height(row_h);
         }
         
         links_h = $('#joblinks .links').height();
         if(links_h > (row_h*1.5)) {
            $('#joblinks .select_more').find('span').text('收起');
            $('#joblinks .select_more').find('i').addClass('up');
         } else {
            $('#joblinks .select_more').find('span').text('更多');
            $('#joblinks .select_more').find('i').removeClass('up');
         }
      }
      
      if($('#resumesearch_wrap ul li.cur_selected').length > 0) {
         $('#resumesearch_wrap').on('click', 'ul li.cur_selected a', function(){
            return false;
         });
      }
   }
   
   buildJobLinks();
   
   $(window).resize(function() {
      buildJobLinks();
   });
   */
   
   $('#resumesearch_wrap').on('click', '#joblinks .select_more', function(){
      var row_h = $('#joblinks .links li').eq(0).height() + 4;
      if($('#joblinks .links').height() > (row_h*1.5)) {
         $(this).find('span').text('更多');
         $(this).find('i').removeClass('up');
         $('#joblinks .links').height(row_h);
         $('#joblinks .links').removeClass('showmorejob');
         //switchAdvancedSearch('showmorejob', 0);
      } else {
         $(this).find('span').text('收起');
         $(this).find('i').addClass('up');
         $('#joblinks .links').height('auto');
         $('#joblinks .links').addClass('showmorejob');
         //switchAdvancedSearch('showmorejob', 1);
      }
   });
   
   // show mode module
   $('#showlist').click(function(){
      $(this).addClass('curselect');
      $('#showdetail').removeClass('curselect');
      $('.control_tr .resumedetails').hide();
      $('.control_trselect').removeClass('tr_hovercolor').find('.clicktoggle').removeClass('clicktoggle_up');
      //switchAdvancedSearch('showdetail', 0);
   });

   function showDetail(resumeList){
      if(resumeList.length > 0) {
         var target = resumeList.shift();
         var detailTarget = target.next().find('.resumedetails');
         var resumeid = target.attr('data-id');
         if(!detailTarget.html()) {
            $.ajax({
               type: "GET",
               url: OC.filePath('resumes', 'ajax', 'getresumedetail.php'),
               data: {resumeid: resumeid},
               dataType: 'html',
               success: function(result){
                  detailTarget.html(result);
                  detailTarget.show();
                  showDetail(resumeList);
               }
            });
         } else {
            detailTarget.show();
            showDetail(resumeList);
         }
         target.addClass('tr_hovercolor').find('.clicktoggle').addClass('clicktoggle_up');
      }
   }
   
   $('#showdetail').click(function(){
      $(this).addClass('curselect');
      $('#showlist').removeClass('curselect');
      $('.control_tr .resumedetails').show();
      $('.control_trselect').addClass('tr_hovercolor').find('.clicktoggle').addClass('clicktoggle_up');
      //switchAdvancedSearch('showdetail', 1);
   });
   
   // resume config info
   $(document).on("click", "#click_howtoconfig", function(){
      var target = $(this);
      $("#hr_howtoconfig").show().position({
         my:"left top+10",
         at:"left-60 bottom",
         of:target
      });
   });
   
   $(document).on('click', '.closediv', function(){
       $(this).closest('.popup').hide();
   });
   
   $("td.resumename a.name").click(function(){
      $(this).closest(".control_trselect").removeClass("bold");
   });

   window.onbeforeunload = function(){
      window.onbeforeunload = null;

      if ($('#searchtoken').length > 0 && $('#searchtoken').val().length > 0) {
         $.ajax({
            async: false,
            type: 'POST',
            url: OC.filePath('resumes', 'ajax', 'emptytoken.php'),
            data: {t: $('#searchtoken').val()},
            dataType: 'json',
            success: function(result){
               // do nothing
            }
         });
      }
   }
   
});

